/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package da;

import entities.LoanContact;
import entities.Payment;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Bich Tran Van
 */
public class DAPayment {
    private DALoanContact daLoanContact = new DALoanContact();
    private ArrayList<LoanContact> listContact;
    MSSQLConnection conn = null;

    public DAPayment() {
        try {
            conn = new MSSQLConnection();
            conn.getConnection();
        } catch (Exception ex) {
            Logger.getLogger(DALoanType.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public int UpdatePayment(int paymentid) throws Exception
    {
        int result = 0;
        try{
            conn.getConnection();
            conn.cstm = conn.connect.prepareCall("{ CAlL Update_Payment(?)}");
            conn.cstm.setInt(1, paymentid);
            result =  conn.cstm.executeUpdate();
            conn.myClose();
        }
        catch(Exception ex){
            
        }
        return result;
        
    }
    
    public ArrayList<Payment> getPayment(int loanID) throws Exception {
        ArrayList<Payment> list = new ArrayList<Payment>();
        try {
            conn.getConnection();
            conn.cstm = conn.connect.prepareCall("{CALL Cal_Fine(?)}");
            conn.cstm.setInt(1, loanID);
            conn.cstm.executeUpdate();
            
            
            conn.cstm = conn.connect.prepareCall("{CALL View_Payment(?)}");
            conn.cstm.setInt(1, loanID);
            conn.rs = conn.cstm.executeQuery();
            while (conn.rs.next()) {
                Payment payment = new Payment();

                payment.setPaymentId(conn.rs.getInt("PaymentID"));
                
                listContact = daLoanContact.getByID(conn.rs.getInt("LoanID"));
                payment.setLoan(listContact.get(0));
                
                payment.setMonth(conn.rs.getInt("Month"));
                payment.setPayment(conn.rs.getDouble("Payment"));
                payment.setInterest(conn.rs.getDouble("Interest"));
                payment.setFineAmount(conn.rs.getDouble("FineAmount"));
                payment.setTotalPay(conn.rs.getDouble("TotalPay"));
                payment.setDureDate(conn.rs.getDate("DureDate"));
                payment.setDatePaid(conn.rs.getDate("DatePaid"));
                payment.setStatus(conn.rs.getBoolean("Status"));
                list.add(payment);
            }
            conn.myClose();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
